// bindCard.js
var app = getApp()
import { showTi, showModal } from "../../utils/modal.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 姓名
    name: '',
    // 银行卡帐户
    account: '',
    // 开户银行
    channel_title: '',
    // 银行支行
    open_bank: '',
    disabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = wx.getStorageSync('wechat_user')
    console.log(user)
  },
  bindHandlerChange: function (e) {
    let type = e.currentTarget.dataset.type
    let obj = {}
    obj[type] = e.detail.value
    this.setData(obj)
  },
  //绑定银行卡
  saveCard: function (e) {
    let self = this
    let params = self.data
    delete params.disabled;
    if (!params.name) {
      showTi('请输入姓名');
    } else if (!(new RegExp(/^(\d{16}|\d{19})$/)).test(params.account)) {
      showTi("请正确输入银行卡号")
    } else if (!params.channel_title) {
      showTi('请输入开户银行');
    } else if (!params.open_bank) {
      showTi('请输入银行支行');
    } else {
      params.channel_type = 'bank';
      app.query('web/users/cash_account', params, 'post').then(res => {
        if (res.code == 200) {
          wx.showModal({
            title: "提示",
            content: '绑定成功',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({ url: '/pages/withdrawals/withdrawals'})
              } 
            }
          })
          console.log(res)
        }else{
          showTi(res.msg)
        }
      });
    }
  },
  show: function (data) {
    showTi(data)
  },
})